#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@File    ：book.py
@Author  ：平
@Date    ：2025/10/2 21:19 
"""
from sqlalchemy import Column, String, Integer, DateTime, Text, func

from app.config.config import Base


class Book(Base):
    __tablename__ = 'book'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='主键id')
    title = Column(String(255), nullable=False, comment='书名')
    description = Column(Text, comment='介绍')
    author = Column(String(255), comment='作者')
    cover_url = Column(String(512), comment='封面url')
    publisher = Column(String(255), comment='出版社')
    publish_year = Column(Integer, comment='出版年份')
    download_count = Column(Integer, default=0, comment='下载次数')
    read_count = Column(Integer, default=0, comment='阅读次数')
    category = Column(String(255), comment="分类(以','隔开)")
    tags = Column(String(255), comment="标签(以','隔开)")
    summary = Column(Text, comment='摘要')
    created_at = Column(DateTime, default=func.current_timestamp(), comment='创建时间')
    updated_at = Column(DateTime, default=func.current_timestamp(), onupdate=func.current_timestamp(),
                        comment='更新时间')

    def to_dict(self):
        return {
            "id": self.id,
            "title": self.title,
            "description": self.description,
            "author": self.author,
            "coverUrl": self.cover_url,
            "publisher": self.publisher,
            "publishYear": self.publish_year,
            "downloadCount": self.download_count,
            "readCount": self.read_count,
            "category": self.category,
            "tags": self.tags,
            "summary": self.summary,
            "createdAt": self.created_at,
            "updatedAt": self.updated_at
        }
